package web.gkw.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import web.gkw.service.ExpertsService;

import com.opensymphony.xwork2.ActionSupport;

@Component
public class Expert extends ActionSupport {

	@Resource
	private ExpertsService expertService;
	private int page;
	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public ExpertsService getExpertService() {
		return expertService;
	}

	public void setExpertService(ExpertsService expertService) {
		this.expertService = expertService;
	}

	private List<web.gkw.model.Experts> experts;

	public List<web.gkw.model.Experts> getExperts() {
		return experts;
	}

	public void setExperts(List<web.gkw.model.Experts> experts) {
		this.experts = experts;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub

		if(page == 0) {
			page = 1;
		}



		experts = expertService.getFirstList(page);
		pages = expertService.getCount();
		makePic(experts);

		return "success";
	}

	public void makePic(List<web.gkw.model.Experts> experts) {
		HttpServletRequest request = ServletActionContext.getRequest();
		String path = request.getSession().getServletContext().getRealPath("");
		path += "/temp/";
		for(web.gkw.model.Experts expert : experts) {
			try {
				byte[] photo = expert.getPhoto();
				//				InputStream in = photo.getBinaryStream();
				String fileName = path + expert.getId() +".jpg";
				File file = new File(fileName);
				FileOutputStream out = null;
				if(!file.exists()) {
					out = new FileOutputStream(file);
					out.write(photo);
					out.flush();
					out.close();

				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
